#include <bits/stdc++.h>
using namespace std;

int t, m;
int tag, x, y;
vector<int> vec;
unordered_map<int, int> mp;

int find(int x){
    if (mp[x] == x)
        return x;
    mp[x] = find(mp[x]);
    return mp[x];
}
void add(int x, int y){
    x = find(x);
    y = find(y);
    mp[x] = y;
}

int main()
{
    bool fail;
    scanf("%d", &t);
    
    for (int i = 0; i < t; i++){
        mp.clear();
        vec.clear();
        scanf("%d", &m);
        for (int j = 0; j < m; j++){
            scanf("%d%d%d", &x, &y, &tag);
            if (!mp.count(x))
                mp[x] = x;
            if (!mp.count(y))
                mp[y] = y;
            if (tag == 1){
                add(x, y);
            }
            else{
                vec.emplace_back(x);
                vec.emplace_back(y);
            }
        }
        int size = vec.size();
        fail = false;
        for (int k = 0; k < size && !fail; k += 2){
            x = vec[k];
            y = vec[k + 1];
            if (find(x) == find(y)){
                fail = true;
                break;
            }
        }
        if (fail)
            cout << "NO" << endl;
        else
            cout << "YES" << endl;
    }
    return 0;
}
